package com.ruoyi.web.controller.uav;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.uav.domain.UavGroundObstacle;
import com.ruoyi.uav.domain.vo.GroundObstacleData;
import com.ruoyi.uav.service.IUavGroundObstacleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 地面障碍物Controller
 *
 * @author ruoyi
 * @date 2024-05-07
 */
@RestController
@RequestMapping("/uav/obstacle")
@Api(value = "地面障碍物控制层", tags = "地面障碍物控制层")
public class UavGroundObstacleController extends BaseController {
    @Resource
    private IUavGroundObstacleService uavGroundObstacleService;

    /**
     * 获取地面障碍物详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:obstacle:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取地面障碍物详细信息", notes = "传入id")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        List<UavGroundObstacle> list = uavGroundObstacleService.list(Wrappers.<UavGroundObstacle>lambdaQuery()
                .eq(UavGroundObstacle::getTrainingProposeId, id));
        list.forEach(item -> item.setId(Long.valueOf(item.getGenerateId())));
        return success(list);
    }

    /**
     * 修改地面障碍物
     */
    @PreAuthorize("@ss.hasPermi('system:obstacle:edit')")
    @Log(title = "地面障碍物", businessType = BusinessType.UPDATE)
    @RepeatSubmit(interval = 2000, message = "请求过于频繁")
    @PostMapping
    @ApiOperation(value = "修改地面障碍物", notes = "传入uavGroundObstacle")
    public AjaxResult edit(@RequestBody GroundObstacleData groundObstacleData) {
        Long id = groundObstacleData.getId();
        uavGroundObstacleService.remove(Wrappers.<UavGroundObstacle>lambdaQuery()
                .eq(UavGroundObstacle::getTrainingProposeId, id));
        List<UavGroundObstacle> obstacleData = groundObstacleData.getData();
        for (UavGroundObstacle item : obstacleData) {
            item.setTrainingProposeId(id);
            item.setGenerateId(item.getId().toString());
            item.setId(null);
        }
        return success(uavGroundObstacleService.saveBatch(obstacleData));
    }


}
